package com.wxm.remotemeter.service.impl;

import java.util.Map;

import com.talkweb.ncfw.data.Page;
import com.talkweb.ncfw.exceptions.BizBaseException;
import com.talkweb.ncfw.exceptions.BizMultiException;
import com.talkweb.ncfw.exceptions.DAOBaseException;
import com.talkweb.ncfw.orm.IGenericDAO;
import com.talkweb.ncfw.utils.AssertUtils;
import com.talkweb.ncfw.utils.PKUtils;
import com.wxm.remotemeter.persistence.entity.MeterType;
import com.wxm.remotemeter.service.IMeterTypeService;

/**
 * <p>文件名称: MeterTypeServiceImpl.java</p>
 * <p>文件描述: 表具类型服务实现类</p>
 * <p>版权所有: 版权所有(C)2010</p>
 * <p>内容摘要: 简要描述本文件的内容，包括主要模块、函数及能的说明</p>
 * <p>其他说明: 其它内容的说明</p>
 * <p>完成日期: 2012-2-19</p>
 * <p>修改记录1:</p>
 * <pre>
 *    修改日期:
 *    修 改 人:
 *    修改内容:
 * </pre>
 * <p>修改记录2：…</p>
 * @author  Wuqingming
 */
public class MeterTypeServiceImpl implements IMeterTypeService {
	private static final String DEFAULT_NAMESPACE = "com.wxm.remotemeter.persistence.dao.MeterTypeMapper";
	private IGenericDAO genericDAO;

	public IGenericDAO getGenericDAO() {
		return genericDAO;
	}

	public void setGenericDAO(IGenericDAO genericDAO) {
		this.genericDAO = genericDAO;
	}

	public void add(MeterType meterType) throws BizBaseException {
		String typeid = PKUtils.getPrimaryKey();
		meterType.setTypeid(typeid);
		try {
			this.genericDAO.insert(DEFAULT_NAMESPACE + ".insert", new Object[]{meterType});
		} catch (DAOBaseException e) {
			throw new BizBaseException("添加表具类型时发生错误", e);
		}
	}

	public void update(MeterType meterType) throws BizBaseException {
		try {
			this.genericDAO.update(DEFAULT_NAMESPACE + ".update", new Object[]{meterType});
		} catch (DAOBaseException e) {
			throw new BizBaseException("修改表具类型时发生错误", e);
		}
	}

	public void deleteBatch(String[] typeidArray) throws BizBaseException {
		AssertUtils.notEmpty(typeidArray, " 表具类型ID不能为空");
		try {
			this.genericDAO.deleteByPkArray(DEFAULT_NAMESPACE + ".deleteByPkArray", typeidArray);
		} catch (DAOBaseException e) {
			if (typeidArray.length == 1) {
				throw new BizBaseException("删除表具类型时发生错误", e);
			} else {
				throw new BizMultiException("删除表具类型时发生错误", e);
			}
		}
	}

	public MeterType get(String typeid) throws BizBaseException {
		Object meterTypeObj = null;
		try {
			meterTypeObj = this.genericDAO.load(DEFAULT_NAMESPACE + ".getByPrimaryKey", typeid);
		} catch (DAOBaseException e) {
			throw new BizBaseException("获取表具类型时发生错误，表具ID[" + typeid + "]", e);
		}
		return meterTypeObj == null ? null : (MeterType)meterTypeObj;
	}

	public Page<MeterType> find(Page<MeterType> page,
			Map<String, Object> condition) throws BizBaseException {
		try {
			return this.genericDAO.find(DEFAULT_NAMESPACE + ".getCount", DEFAULT_NAMESPACE + ".find", 
					page, condition);
		} catch (DAOBaseException e) {
			throw new BizBaseException("查询表具类型时发生错误", e);
		}
	}

}

